<?php

require_once('admincontroller.php');

class Menus extends AdminController
{
	function Menus()
	{
		parent::AdminController();
		$this->load->model('Menu');
		$this->load->model('MenuItem');
		$this->load->model('Content');
		$this->data = array();
		$this->main_navigation = 'site';
		$this->sub_navigation =  'menus';
	}

	function index()
	{
		$data['menus'] = $this->Menu->all();
		$this->display('menu_list', $data);
	}

	function edit($id)
	{
		$data['menu'] = $this->Menu->get_by_id($id);
		$data['menu_items'] = $this->MenuItem->get_by_menu_id($id);
		$this->display('menu_edit', $data);
	}

	function get_item($id)
	{
		$this->_load_item_resources();
		$this->data['item'] = $this->MenuItem->get_by_id($id);
		$this->load->view('admin/menus/_menu_item_form', $this->data);
	}

	function blank_item($menu_id, $parent_item_id)
	{
		$this->_load_item_resources();
		$this->data['item'] = new MenuItem();
		$this->data['item']->menu_id = $menu_id;
		$this->data['item']->menu_item_id = $parent_item_id;
		$this->load->view('admin/menus/_menu_item_form', $this->data);
	}

	function create_item()
	{
		$item = new MenuItem();
		$this->_save_item($item);
	}

	function update_item($id)
	{
		$item = $this->MenuItem->get_by_id($id);
		$this->_save_item($item);
	}

	function update_items_positions()
	{
		$parent_item_id = isset($_POST['parent_item_id']) ? intval($_POST['parent_item_id']) : null;

		$position = 1;
		foreach ($_POST['menuitem'] as $menu_item_id) {
			$menu_item = $this->MenuItem->get_by_id($menu_item_id);
			if ($menu_item->position != $position || $menu_item->menu_item_id != $parent_item_id) {
				$menu_item->position = $position;
				$menu_item->menu_item_id = $parent_item_id;
				if (!$this->MenuItem->save($menu_item)) {
					$this->output->set_status_header(400);
				break;
				}
			}
			++$position;
		}
	}

	function delete_item($id)
	{
		if (!$this->MenuItem->delete($id)) {
			$this->output->set_status_header(400);
		}
	}

	/**
	 * Charge les ressources pouvant être associées à un menu_item
	 */
	private function _load_item_resources()
	{
		$this->load->model('PublicPage');

		// liste des pages de contenu disponibles
		$contents = array();
		foreach ($this->Content->all() as $content) {
			// content_id => Titre par défaut
			$contents[$content->id] = $content->title;
		}
		$this->data['contents'] = $contents;

		// liste des pages publiques disponibles sur le front office
		$public_pages = array();
		foreach ($this->PublicPage->all() as $public_page) {
			// url => Titre par défaut
			$public_pages[$public_page->url] = $public_page->title;
		}
		$this->data['intern_links'] = $public_pages;
	}

	/**
	 * Sauvegarde d'un menu_item depuis les données $_POST
	 */
	private function _save_item(&$item)
	{
		$item->name = clean_str($_POST['name']);
		switch ($_POST['link_type'])
		{
			case 'content_link':
				$item->content_id = intval($_POST['content_link']);
				$item->url = '';
				break;
			case 'intern_link':
				$item->content_id = null;
				$item->url = clean_str($_POST['intern_link']);
				break;
			case 'extern_link':
				$item->content_id = null;
				$item->url = clean_str($_POST['extern_link']);
				// force http:// if protocol is omitted
				if (!$this->MenuItem->is_extern_link($item)) {
					$item->url = 'http://' . $item->url;
				}
				break;
			case 'no_link':
				$item->url = '';
				$item->content_id = null;
				break;
			default:
				$this->output->set_status_header(400);
				return;
		}
		$item->online = isset($_POST['online']);
		$item->menu_id = intval($_POST['menu_id']);
		$menu_item_id = intval($_POST['menu_item_id']);
		$item->menu_item_id = $menu_item_id == 0 ? null : $menu_item_id;
		if (!$this->MenuItem->save($item)) {
			$this->output->set_status_header(400);
		}
		else {
			// on recharge le contenu de la page des menu items
			$data['menu'] = $this->Menu->get_by_id($item->menu_id);
			$data['menu_items'] = $this->MenuItem->get_by_menu_id($item->menu_id);
			$this->load->view('admin/menus/_menu_items', $data);
		}
	}
}
